package com.sgcc.ln.pmos.mobileapp.dialog;

import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.sgcc.ln.pmos.common.http.HttpInterConst;
import com.sgcc.ln.pmos.common.http.HttpRequestUtils;
import com.sgcc.ln.pmos.common.http.MvcNetCallback;
import com.sgcc.ln.pmos.common.listener.OnItemClickListener;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.mobileapp.adapter.DirectoryAdapter;
import com.sgcc.ln.pmos.mobileapp.adapter.ListAdapter;
import com.sgcc.ln.pmos.mobileapp.bean.GeographicregionBean;
import com.sgcc.ln.pmos.mobileapp.bean.StringDataBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 地理地区
 */
public class GeographicalAreaPopup extends BottomPopupView {

    private RelativeLayout mRlCancle;
    private RelativeLayout mRlDetermine;
    private RecyclerView mRvDirectory;
    private RecyclerView mRvList;
    private Context mContext;
    private DirectoryAdapter mDirectoryAdapter;
    private ListAdapter mListAdapter;
    private List<GeographicregionBean.DataBean> dirList = new ArrayList<>();
    private List<GeographicregionBean.DataBean> list_list = new ArrayList<>();

    public GeographicalAreaPopup(@NonNull Context context, OnCallBackListener onCallBackListener) {
        super(context);
        mContext = context;
        mOnCallBackListener = onCallBackListener;
    }

    public interface OnCallBackListener {
        void save(String address, String geogrRegionId,GeographicregionBean.DataBean dataBean);
    }

    private OnCallBackListener mOnCallBackListener;

    @Override
    protected int getImplLayoutId() {
        return R.layout.custombottom_geographicalarea_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        mRlCancle = (RelativeLayout) findViewById(R.id.rl_cancle);
        mRlDetermine = (RelativeLayout) findViewById(R.id.rl_determine);
        mRvDirectory = (RecyclerView) findViewById(R.id.rv_directory);
        mRvList = (RecyclerView) findViewById(R.id.rv_list);

        mRlCancle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        mRlDetermine.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                queryName(dirList.get(dirList.size() - 1).getGeogrRegionId());
            }
        });

        LinearLayoutManager mLayoutManagerDirectory = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        mRvDirectory.setLayoutManager(mLayoutManagerDirectory);
        mDirectoryAdapter = new DirectoryAdapter();
        mDirectoryAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(Object bean, int position) {
                GeographicregionBean.DataBean dataBean = (GeographicregionBean.DataBean) bean;
                dirList = dirList.subList(0, position + 1);
                mDirectoryAdapter.setList(dirList);
                queryAllGeographicregion(dataBean.getGeogrRegionId());
            }
        });
        mRvDirectory.setAdapter(mDirectoryAdapter);

        LinearLayoutManager mLayoutManagerList = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mRvList.setLayoutManager(mLayoutManagerList);
        mListAdapter = new ListAdapter();
        mListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(Object bean, int position) {
                GeographicregionBean.DataBean dataBean = (GeographicregionBean.DataBean) bean;
                queryAllGeographicregion(dataBean.getGeogrRegionId());
                if (!onData2Find(dataBean)) {
                    dirList.add(dataBean);
                }
                mDirectoryAdapter.setList(dirList);
            }
        });
        mRvList.setAdapter(mListAdapter);

        queryAllGeographicregion("");
    }

    private boolean onData2Find(GeographicregionBean.DataBean bean) {
        for (GeographicregionBean.DataBean dataBean : dirList) {
            if (dataBean.getGeogrRegionId().equals(bean.getGeogrRegionId())) {
                return true;
            }
        }
        return false;
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
    }

    //完全消失执行
    @Override
    protected void onDismiss() {
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .7f);
    }

    @Override
    protected boolean onBackPressed() {
        return true;
    }

    private void queryAllGeographicregion(String geogrRegionId) {
        Map<String, Object> params = new HashMap<>();
        params.put("geogrRegionId", geogrRegionId);
        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERYALLGEOGRAPHICREGION, new MvcNetCallback<GeographicregionBean>(true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);

            }

            @Override
            public void onResponse(GeographicregionBean response, int id) {
                super.onResponse(response, id);

                if (response != null) {
                    if (response.getStatus() == 0 && "Success".equals(response.getMessage())) {
                        if (response.getData().isEmpty()) {
                            mRlDetermine.setVisibility(VISIBLE);
                        } else {
                            mRlDetermine.setVisibility(GONE);
                        }


                        if (dirList.size() == 0) {
                            dirList.add(response.getData().get(0));
                        }

                        list_list.clear();
                        list_list.addAll(response.getData());

                        mDirectoryAdapter.setList(dirList);
                        mListAdapter.setList(list_list);
                    }
                }
            }
        });
    }

    private void queryName(String geogrRegionId) {
        Map<String, Object> params = new HashMap<>();
        params.put("geogrRegionId", geogrRegionId);
        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERYNAME, new MvcNetCallback<StringDataBean>(true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);

            }

            @Override
            public void onResponse(StringDataBean response, int id) {
                super.onResponse(response, id);

                if (response != null) {
                    if (response.getStatus() == 0 && "Success".equals(response.getMessage())) {
                        if (mOnCallBackListener != null) {
                            String geogrRegionId = dirList.get(dirList.size() - 1).getGeogrRegionId();
                            mOnCallBackListener.save(response.getData(), geogrRegionId,dirList.get(dirList.size() - 1));
                            dismiss();
                        }
                    }
                }
            }
        });
    }
}
